﻿# coding: utf-8
import os
import fnmatch

import wx
import wx.lib.scrolledpanel as scrolled

class wxPanelPropertyParticleTexture(scrolled.ScrolledPanel):
    def __init__(self, parent, id):
        """ Default class constructor """
        
        scrolled.ScrolledPanel.__init__(self, parent, id)
        
        staticParticleTexture = wx.StaticText(self, -1, TD.PARTICLE_TEXTURE)
        self._texturePath = wx.TextCtrl(self, wx.NewId(), '')
                
        staticParticleTextureCutting = wx.StaticText(self, -1, TD.PARTICLE_TEXTURE_CUTTING)
        self._textureCutting = wx.TextCtrl(self, wx.NewId(), '')

        self._treeParticleTexture = wx.TreeCtrl(self, wx.NewId(), style = wx.TR_HAS_BUTTONS)
        
        sizerTexturePath = wx.BoxSizer(wx.HORIZONTAL)
        sizerTexturePath.Add(staticParticleTexture, 0, wx.CENTER, border = 5)
        sizerTexturePath.Add(self._texturePath, 1, wx.ALL|wx.EXPAND, border = 5)
                
        sizerTextureCutting = wx.BoxSizer(wx.HORIZONTAL)
        sizerTextureCutting.Add(staticParticleTextureCutting, 0, wx.CENTER, border = 5)
        sizerTextureCutting.Add(self._textureCutting, 1, wx.ALL|wx.EXPAND, border = 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(sizerTexturePath, 0, wx.ALL|wx.EXPAND, border = 5)
        sizer.Add(sizerTextureCutting, 0, wx.ALL|wx.EXPAND, border = 5)
        sizer.Add(self._treeParticleTexture, 1, wx.ALL|wx.EXPAND, border = 5)
        self.SetSizer(sizer)
                
        il = wx.ImageList(IL.IMAGE_SMALL, IL.IMAGE_SMALL)
        self._bmpIdxFolderNormal = il.Add(IL.GetBitmap(IL.ID_IMAGE_FILE_FOLDER_CLOSE, IL.IMAGE_SMALL))
        self._bmpIdxFolderExpand = il.Add(IL.GetBitmap(IL.ID_IMAGE_FILE_FOLDER_OPEN, IL.IMAGE_SMALL))
        self._bmpIdxFile = il.Add(IL.GetBitmap(IL.ID_IMAGE_FILE_NORMAL, IL.IMAGE_SMALL))
        self._bmpIdxFileSel = il.Add(IL.GetBitmap(IL.ID_IMAGE_FILE_SELECTED, IL.IMAGE_SMALL))
        self._bmpIdxType = il.Add(IL.GetBitmap(IL.ID_IMAGE_ASSETS_TYPE, IL.IMAGE_SMALL))
        self._bmpIdxRoot = il.Add(IL.GetBitmap(IL.ID_IMAGE_ASSETS, IL.IMAGE_SMALL))
        self._treeParticleTexture.SetImageList(il)
        self._il = il

        self._root = self._treeParticleTexture.AddRoot(TD.PARTICLE_TEXTURE)
        self._treeParticleTexture.SetItemImage(self._root, self._bmpIdxRoot, wx.TreeItemIcon_Normal)
        
        self._texturePath.Bind(wx.EVT_KILL_FOCUS, self.__OnTextureChanged)
        self._textureCutting.Bind(wx.EVT_KILL_FOCUS, self.__OnEnterTextureCutting, self._textureCutting)
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.__OnTextureSelChanged, self._treeParticleTexture)
        self._treeParticleTexture.Bind(wx.EVT_KEY_DOWN, self.__OnTreeKeyDown)
        
    def __OnTreeKeyDown(self, e):
        code = e.GetKeyCode()
        if code == wx.WXK_F5:
            self.__OnUpdateTreeList()
                    
    def UpdateParticleTextureInformation(self):
        self.__UpdateTextureCutting()
        self.__UpdateTextureFileName()
        
    def __UpdateTextureCutting(self):    
        cutting = PARTICLE_SYSTEM_EDITOR.GetTextureCutting()
        self._textureCutting.SetValue(str(cutting))
        
    def __UpdateTextureFileName(self):
        textureFileName = PARTICLE_SYSTEM_EDITOR.GetTextureFileName()
        self._texturePath.SetValue(textureFileName)
    
    def __OnEnterTextureCutting(self, e):
        try:
            edit = e.GetEventObject()
            cutting = abs(int(edit.GetValue()))
            PARTICLE_SYSTEM_EDITOR.SetTextureCutting(cutting)
            
        finally:
            self.__UpdateTextureCutting()
        
    def __OnTextureChanged(self, e):
        path = self._texturePath.GetValue()
        PARTICLE_SYSTEM_EDITOR.SetTextureName(path)
    
    def __OnTextureSelChanged(self, e):
        item = e.GetItem()
        data = self._treeParticleTexture.GetItemPyData(item)
        if data:
            texture_path = REMOVE_ROOT_PATH(data)
            PARTICLE_SYSTEM_EDITOR.SetTextureName(texture_path)

        self.__UpdateTextureFileName()
        
    def __OnBnClickRefresh(self, e):
        self.__OnUpdateTreeList()
    
    def __OnUpdateTreeList(self):
        self._treeParticleTexture.DeleteChildren(self._root)
        root_path = GLOBAL_SETTING._workDir + GLOBAL_SETTING._textureDir
        self.__WalkDirectoryTree(self._root, root_path, ['*.bmp', '*.tga', '*.png', '*.jpg', '*.dds'])
        self._treeParticleTexture.Expand(self._root)
        
    def __WalkDirectoryTree(self, node, root, ext):
        listDir = os.listdir(root)
        for d in listDir:
            fullpath = os.path.join(root, d)
            if os.path.isdir(fullpath):
                subnode = self._treeParticleTexture.AppendItem(node, d)
                self._treeParticleTexture.SetItemImage(subnode, self._bmpIdxFolderNormal, wx.TreeItemIcon_Normal)
                self._treeParticleTexture.SetItemImage(subnode, self._bmpIdxFolderExpand, wx.TreeItemIcon_Expanded)
                self.__WalkDirectoryTree(subnode, fullpath, ext)
            elif os.path.isfile(fullpath):
                for pat in ext:
                    if fnmatch.fnmatch(d, pat):
                        fnode = self._treeParticleTexture.AppendItem(node, d)
                        self._treeParticleTexture.SetPyData(fnode, fullpath)
                        self._treeParticleTexture.SetItemImage(fnode, self._bmpIdxFile, wx.TreeItemIcon_Normal)
                        self._treeParticleTexture.SetItemImage(fnode, self._bmpIdxFileSel, wx.TreeItemIcon_Selected)
                        break        
                